package servlets;

import java.io.IOException;
import javaPersistence.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.FormData;

/**
 * Servlet implementation class LoadDataUser
 */
@WebServlet("/LoadUser")
public class LoadDataUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoadDataUser() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int code = 0;
		String name = "";
		String lastName = "";
		String email = "";
		String pass = "";
		try {
			HttpSession sess = request.getSession();
			User usr = new User();
			FormData fdata = new FormData();
			code = Integer.parseInt(request.getParameter("code"));
			name = request.getParameter("firstname");
			lastName = request.getParameter("lastname");
			email = request.getParameter("email");
			pass = request.getParameter("pass");
			System.out.println("code:"+code+" name:"+name+" lastName:"+lastName+" email:"+email+" pass:"+pass);
			if(code > 0 && name != null && lastName != null && email != null && pass != null){
				if(!usr.insertUser(code, name, lastName, email, pass)){
					fdata.setError("Error: Los datos insertados ya existen o estan mal diligenciados.");
				}else{
					fdata.setMsgOK("OK: Los datos fueron almacenados correctamente.");
				}
			}else{
				fdata.setError("TODOS: Los datos son necesarios");
			}
			sess.setAttribute("FormData", fdata);
			response.sendRedirect(request.getContextPath() + "/jsps/negocio/usuarios.jsp");
		} catch (Exception e) {
		}
	}

}
